/*
 * i18n movie detail
 */
$(function() {
	show_detail.run();

});

var show_detail = {
	run : function() {
		this.__init__();
		this._start_();
		this._bind_events();
	},

	__init__ : function() {
		this.contextPath = $("#contextPath").val() || "";
		this.showId = $("#showId").val();
		this.type = $("#type").val();
		this.category = $("#category").val();
		this.itemUrl = $("#itemUrl").val();
		this.title = $("#title").val();
		this.source = $("#source").val();
		this.vdInstalled = vd.check();
		this.genre ='';
		this.decade ='';
	},
	
	_start_ : function() {
		share.run();
		show_detail._highlight_nav();
		
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			var params = loc.substring(index + 1);
			this._highlight_genre(params);
		}
	},
	
	_bind_events : function() {
		// play now btn
		$("#play_btn").click(function() {
			var title = show_detail.title;
			_gaq.push([ '_trackEvent', 'movie', 'Play', title ]);
			// main.addOrUpdateViewHistory(show_detail.category,show_detail.title,show_detail.showId);
			var url = show_detail.contextPath + "/world/movie/" + show_detail.category + "/watch-" + show_detail.showId;
			if(show_detail.genre){
				url += "#genre=" + show_detail.genre;
			}
			window.location.href = url;
			return false;
		});
		
		var locaIndex = location.href.indexOf("#"); 
		var localUrl = location.href;
		if(locaIndex > 0){
			localUrl = localUrl.substring(0,locaIndex);
		}
		$("#fb-comments").attr("data-href", localUrl);
		
		//genre
		$("#genres_ul > li > a").click(function(){
			var $this = $(this);
			var g = $this.attr("genre");
			location.href = main.contextPath + "/world/movie/" + show_detail.category + "s#genre=" + g; 
			return false;
		});
	},
	
	_highlight_nav: function(){
		var index = 0;
		if(this.category == 'trailer'){
			index = 1;
		}
		$("#side_menu > ul > li").eq(index).addClass("curr");		
	},
	
	_highlight_genre: function(params){
		var paramArr = params.split("&");
		for(var i=0; i<paramArr.length;i++){
			if (paramArr[i].indexOf("genre") >= 0) {
				var value = paramArr[i].substring(paramArr[i].indexOf("=") + 1);
				$("#genres_ul > li > a").removeClass("selected");
				$("#genres_ul > li > a").each(function() {
					if ($(this).attr("genre") == decodeURI(value)) {
						$(this).addClass("selected");
						show_detail.genre = $(this).attr("genre");
					}
				});
			}
		}
	},
	
};

var share = {
	run : function() {

		var url = location.href;
		$("#share_input").val(url);

		$("#fb_share_a").click(function() {
			var fbUrl = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(url);
			_gaq.push([ '_trackEvent', 'Movie', 'fb_share', url ]);
			window.open(fbUrl);
		});

		$("#tw_share_a").click(function() {
			var twUrl = "https://twitter.com/share?url=" + encodeURIComponent(url);
			_gaq.push([ '_trackEvent', 'Movie', 'twitter_share', url ]);
			window.open(twUrl);
		});
	}
};
